package com.google.code.greenplate;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class LiveRides extends ListActivity {
	
	String[] items = {"bus", "train", "share ride offer", "share ride request"};  

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.liverides);
		setListAdapter(new IconicAdapter(this));  
	}

	public void onListItemClick(ListView parent, View v, int position, long  id) { 
		String selection = parent.getItemAtPosition(position).toString();

		if (selection == "bus") {
			Toast.makeText(this, "Not implemented", Toast.LENGTH_SHORT).show();
		} else if (selection == "train") {
			Toast.makeText(this, "Not implemented", Toast.LENGTH_SHORT).show();
		} else if (selection == "share ride offer") {
			startActivity(new Intent(LiveRides.this, ShareRideOffer.class));			
		} else if (selection == "share ride request") {
			startActivity(new Intent(LiveRides.this, ShareRideRequest.class));
		}
	} 

	class IconicAdapter extends ArrayAdapter<String> {
		private Activity context; 
		private int[] colors = new int[] { 0x30ffffff, 0x30808080 };

		public IconicAdapter(Activity context) { 
			super(context, R.layout.liverides_listitem, items); 
			this.context = context; 
		} 

		public View getView(int position, View convertView, ViewGroup parent) { 
			LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
			View row = inflater.inflate(R.layout.liverides_listitem, null);

			TextView label = (TextView)row.findViewById(R.id.label); 
			label.setText(items[position]); 
			int colorPos = position % colors.length;
			row.setBackgroundColor(colors[colorPos]);

			if (items[position] == "bus") { 
				ImageView icon = (ImageView)row.findViewById(R.id.icon); 
				icon.setImageResource(R.drawable.bus_48); 
			}    
			else if (items[position] == "train"){
				ImageView icon = (ImageView)row.findViewById(R.id.icon); 
				icon.setImageResource(R.drawable.hot_train_48); 
			}
			else if (items[position] == "share ride offer"){
				ImageView icon = (ImageView)row.findViewById(R.id.icon); 
				icon.setImageResource(R.drawable.classic_truck_48); 
			}
			else if (items[position] == "share ride request"){
				ImageView icon = (ImageView)row.findViewById(R.id.icon); 
				icon.setImageResource(R.drawable.childhood_dream_48); 
			}
			
			return(row); 
		} 
	}  
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.menu, menu);
	    return true;
	}
	
}